﻿param($meteringDuration,$targetAddressListFilePath,$environmentShortName)

$parentPath=(Split-Path $PSCommandPath -Parent)

Import-Module -Force -DisableNameChecking $parentPath\..\BuildDeployment\libAssertion.psm1



$targetAddresses=Get-Content -Path $targetAddressListFilePath


$scriptBlock= 
    {
        param($targetAddress)

        Import-Module $using:parentPath\libNetworking.psm1

        while($true)
        {
            Get-NetworkDeviceResponseTime -targetDeviceAddress $targetAddress -durationInSecond $using:meteringDuration | Store-NetworkDeviceResponseTime -environmentShortName $using:environmentShortName
        }

    }

foreach($targetAddress in $targetAddresses)
{
     
    $jobName="Capture network response time for $targetAddress"

    $jobStarted=$false

    while(-not $jobStarted)
    {
        $activeJobCount=(Get-Job -Name "Capture network response time for *" | Where {$_.State -eq "Running"} | Measure-Object).Count
        if($activeJobCount -le 30)
        {
            Start-Job -Name $jobName -ScriptBlock $scriptBlock -ArgumentList $targetAddress
            $jobStarted=$true
        }
        else
        {
            Start-Sleep -Seconds 5
        }
    }
    
}


do
{
    Get-Job "Capture network response time for *"| Receive-Job
    Get-Job "Capture network response time for *" | where {$_.State -eq "Completed"} | Remove-Job
    Start-Sleep -Seconds 15   
}
while(((Get-Job -Name "Capture network response time for *" | Where {$_.State -eq "Running"} | Measure-Object).Count) -gt 0)
